<?php
namespace app\api\controller\zfshop;
class Userext extends Base
{

    public function __construct() {
        parent::__construct();
        if(!$this->userinfo){
            if(is_post()){
                err('请先登录!',200);
            }
        }
    }

    //  店铺详情
    public function details(){
        $info = $this->db->query("select id as shop_id,username,pic,introduce from user_ext where user_id=? and status=0",[$this->userinfo["id"]],1);
        return suc($info);
    }

    //编辑店铺
    public function edit(){
        $this->checkLogin();
        $d = $this->post();
        $rule = [
            'ext'=>'require',
            'value'=>'require'
        ];
        $msg = [];
        check($rule,$msg,$d);
        if(!in_array($d["ext"],['pic','username','introduce'])){
            err("参数错误");
        }
        $data = [
            $d["ext"] => $d["value"],
        ];
        $rs = $this->db->update("user_ext",$data,["user_id=? and id=?",[$this->userinfo["id"],$this->shopinfo["id"]]]);
        return suc("保存成功");
    }

    //  数量统计
    public function numberStatistics(){
        $this->checkLogin();
        //0消费者  1|vip  2|零售商  3|批发商  4|代理商
        //下载数量
        $son = $this->db->query("select count(*) num from user where s_id = ? and status=0 and level<>0",[$this->userinfo["id"]],1);
        $list = $this->db->query("select level from user where s_id = ? and status=0",[$this->userinfo["id"]]);
        $son0 = 0;
        $son1 = 0;
        $son2 = 0;
        $son3 = 0;
        $son4 = 0;
        foreach ($list as $k => $v) {
            switch ($v["level"]) {
                case 0:
                    $son0++;
                    break;
                case 1:
                    $son1++;
                    break;
                case 2:
                    $son2++;
                    break;
                case 3:
                    $son3++;
                    break;
                case 4:
                    $son4++;
                    break;
                default:
                    break;
            }
        }
        $data = [
            "son"=>$son["num"],
            "son0"=>$son0,
            "son1"=>$son1,
            "son2"=>$son2,
            "son3"=>$son3,
            "son4"=>$son4,
        ];
        return suc($data);
    }

    //我的下级
    public function mySon(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
            'page.require' => 'page不能为空',
            'limit.require' => 'limit不能为空',
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","s_id","=",$this->userinfo["id"]];
        $map[] = ["and","level","<>",0];
        $map[] = ["and","status","=",0];
        $w = where($map);
        $rs = $this->db->query("select username,pic,phone,create_time from user {$w[0]} order by id desc".limit($d['page'],$d['limit']),$w[1],'',true);
        foreach ($rs[0] as $k => $v) {
            $rs[0][$k]["create_time_name"] = com_time_format($v['create_time']);
            $rs[0][$k]["createtime"] = com_time_format($v['create_time']);
        }
        if($rs){
            suc($rs[0],$rs[1]);
        }
        suc();
    }

    //我的服务商
    public function myServiceProvider(){

    }


}
